package com.atguigu.senior;

import com.atguigu.senior.dao.BankDao;
import com.atguigu.senior.dao.BaseDAO;
import com.atguigu.senior.dao.EmployeeDao;
import com.atguigu.senior.dao.impl.BankDaoImpl;
import com.atguigu.senior.dao.impl.EmployeeDaoImpl;
import com.atguigu.senior.pojo.Employee;
import com.atguigu.senior.util.JDBCUtil;
import com.atguigu.senior.util.JDBCUtilV2;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class JDBCUtilTest {
    @Test
    public void testGetConnection() throws Exception {
        Connection connection = JDBCUtil.getConnection();
        System.out.println(connection);
        JDBCUtil.release(connection);
    }

    @Test
    public void testJDBCV2() throws Exception {
        // V1测试
//        Connection connection1 = JDBCUtil.getConnection();
//        Connection connection2 = JDBCUtil.getConnection();
//        Connection connection3 = JDBCUtil.getConnection();
//        System.out.println(connection1);
//        System.out.println(connection2);
//        System.out.println(connection3);
//        JDBCUtil.release(connection1);
//        JDBCUtil.release(connection2);
//        JDBCUtil.release(connection3);
        /**
         * com.mysql.cj.jdbc.ConnectionImpl@82ea68c
         * com.mysql.cj.jdbc.ConnectionImpl@59e505b2
         * com.mysql.cj.jdbc.ConnectionImpl@3af0a9da
         */

        // V2测试
        Connection connection1 = JDBCUtilV2.getConnection();
        Connection connection2 = JDBCUtilV2.getConnection();
        Connection connection3 = JDBCUtilV2.getConnection();
        System.out.println(connection1);
        System.out.println(connection2);
        System.out.println(connection3);
        JDBCUtilV2.release();
        /**
         * com.mysql.cj.jdbc.ConnectionImpl@82ea68c
         * com.mysql.cj.jdbc.ConnectionImpl@82ea68c
         * com.mysql.cj.jdbc.ConnectionImpl@82ea68c
         */
    }


    @Test
    public void testEmployeeDaoSelectAll() throws Exception {
        // 1. 创建DAO实现类对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        // 2. 查询所有员工信息
        List<Employee> employeeList = employeeDao.selectAll();
        employeeList.stream().forEach(System.out::println);
    }


    @Test
    public void testEmployeeDaoSelectById() throws Exception {
        // 1. 创建DAO实现类对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        // 2. 查询指定id的员工信息
        Employee employeeList = employeeDao.selectByEmpId(1);
        System.out.println(employeeList);
    }

    @Test
    public void testEmployeeDaoInsert() throws Exception {
        // 1. 创建DAO实现类对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        // 2. 新增员工信息
        Employee xm = new Employee(null, "小舞", 6500.00, 16);
        int row = employeeDao.insert(xm);
        System.out.println(row);
    }

    @Test
    public void testEmployeeDaoUpdate() throws Exception {
        // 1. 创建DAO实现类对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        // 2. 修改指定员工的信息
        Employee xm = new Employee(1, "小明", 5000.0, 20);
        int row = employeeDao.update(xm);
        System.out.println(row);
    }

    @Test
    public void testEmployeeDaoDelete() throws Exception {
        // 1. 创建DAO实现类对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        // 2. 删除指定ID员工的信息
        int row = employeeDao.delete(1);
        System.out.println(row);
    }

    @Test
    public void testTransaction() {
        BankDao bankDao = new BankDaoImpl();
        Connection connection = null;
        try {
            connection = JDBCUtilV2.getConnection();
            // 手动开启事务
            connection.setAutoCommit(false);

            bankDao.addMoney(1, 500);
            // 抛出一个异常
            int i = 10 / 0;
            bankDao.subMoney(2, 500);

            // 手动提交事务
            connection.commit();
        } catch (Exception e) {
            try {
                // 出现异常后回滚事务
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            // 释放连接
            JDBCUtilV2.release();
        }
    }
}
